// pages/userBind/userBind.js
Page({
  data:{

    multiArray: [],
    multiIndex: 0,
    userInfo:[],

    communityArray:[],
    communityIndex:0,
  },
  formSubmit(e){
    let that=this;
    console.log(e);
    let communityName=this.data.communityArray[this.data.communityIndex];
    let buildingName=this.data.multiArray[this.data.multiIndex];
    let ownerName=e.detail.value.ownerName;
    let profession=e.detail.value.profession;
    let telephone=e.detail.value.telephone;
    let idcard=e.detail.value.idcard;
    let sex=e.detail.value.sex;
    let type=e.detail.value.type;
    wx.request({
      url: 'http://127.0.0.1/owner/bindOwner',
      data:{
        communityName:communityName,
        buildingName:buildingName,
        ownerName:ownerName,
        profession:profession,
        telephone:telephone,
        idcard:idcard,
        sex:sex,
        type:type
      },
      success(result){
        if(result.data.code=='1'){
          wx.showToast({
            title: result.data.msg,
          })
          
          //将用户的信息存储至本地
          wx.setStorageSync('userInfo', JSON.stringify(result.data.data))
          //跳转至个人中心
          wx.reLaunch({
            url: '../my/my',
          })

        }else{
          wx.showToast({
            title: result.data.msg,
          })
        }

      }
    })
    

  },

  onLoad(options){
    let userInfo=wx.getStorageSync('userInfo');

    if(userInfo){
      userInfo =JSON.parse(userInfo);
       this.setData({
      userInfo:userInfo
    })
  }
    let that=this;
    wx.request({
      url: 'http://localhost/owner/getCommunityName',
      success:function(result){
        console.log("111");
        console.log(result.data);
        that.setData({
          communityArray:result.data
        })
      }
    });

    wx.request({
      url: 'http://localhost/owner/getBuildingName',
      success:function(result){
        console.log(result.data);
        that.setData({
          multiArray:result.data
        })
      }
    });

  },

  //小区选择
  bindCommunityPickerChange:function(e){
    console.log(e);
    this.setData({
      communityIndex:e.detail.value
    })
    console.log(this.data.communityArray[this.data.communityIndex]);
  },
  // 房号选择
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value
    })
    console.log(this.data.multiArray[this.data.multiIndex]);
  },

  // 性别选择
  radioChange: function(e){
    console.log(e);
    this.setData({
      sex:e.detail.value
    })
  },
  
 
  

})